﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XStudio.XCRF.Data.Entities;
using System.Linq.Expressions;
using System.Data;
using System.Data.Entity.Infrastructure;
using XStudio.XCRF.Data.Enums;


namespace XStudio.XCRF.Data.Business
{
    public static class MemberBusiness
    {
        /// <summary>
        /// 查找用户
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <returns></returns>
        public static Member Find(this IQueryable<Member> Members, string loginName)
        {
            try
            {
                return Members.Find(m => m.LoginName == loginName).First();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 判断系统中是否有用户
        /// </summary>
        /// <returns></returns>
        public static bool HasMembers(this IQueryable<Member> Members)
        {
            return Members.GetMasterRecords().Any();
        }
        /// <summary>
        /// 判断系统中是否拥有管理员账户
        /// <para>若系统中无管理员账户，则说明数据库出现问题，站点将无法管理，请立即维护</para>
        /// </summary>
        /// <returns></returns>
        public static bool HasAdministrator(this IQueryable<Member> Members)
        {
            return Members.Exist(m => m.Type == (byte)MemberTypes.Administrator);
        }
        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <param name="loginName">登陆名</param>
        /// <returns></returns>
        public static bool Exist(this IQueryable<Member> Members, string loginName)
        {
            return Members.Exist(member => member.LoginName == loginName);
        }
    }
}
